//
//  UIDatePicker+Extension.m
//  FCMainFrameWork
//
//  Created by 陈文斌 on 2020/10/10.
//

#import "UIDatePicker+Extension.h"
#import <objc/runtime.h>
#import "WJCDefine.h"

@implementation UIDatePicker (Extension)
+(void)load{
    
        //只执行一次这个方法
    
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        Class class = [self class];

        //替换两个初始化方法
        SEL originalSelector = @selector(init);
        SEL originalSelector2 = @selector(initWithFrame:);
        
        SEL swizzledSelector = @selector(FCBaseInit);
        SEL swizzledSelector2 = @selector(FCBaseInitWithFrame:);
        
        Method originalMethod = class_getInstanceMethod(class, originalSelector);
        Method originalMethod2 = class_getInstanceMethod(class, originalSelector2);
        
        Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
        Method swizzledMethod2 = class_getInstanceMethod(class, swizzledSelector2);
        
        BOOL didAddMethod = class_addMethod(class,
                                            originalSelector,
                                            method_getImplementation(swizzledMethod),
                                            method_getTypeEncoding(swizzledMethod));
        
        BOOL didAddMethod2 = class_addMethod(class,
                                             originalSelector2,
                                             method_getImplementation(swizzledMethod2),
                                             method_getTypeEncoding(swizzledMethod2));
        
        if (didAddMethod) {
            class_replaceMethod(class,
                                swizzledSelector,
                                method_getImplementation(originalMethod),
                                method_getTypeEncoding(originalMethod));
        } else {
            method_exchangeImplementations(originalMethod, swizzledMethod);
            
        }
        
        if (didAddMethod2) {
            class_replaceMethod(class,
                                swizzledSelector2,
                                method_getImplementation(originalMethod2),
                                method_getTypeEncoding(originalMethod2));
        }else {
            method_exchangeImplementations(originalMethod2, swizzledMethod2);
            
        }
        
    });
    
}

- (instancetype)FCBaseInit
{
    id __self = [self FCBaseInit];
    
    if (@available(iOS 13.4, *)) {
        self.preferredDatePickerStyle = UIDatePickerStyleWheels;
        
    }
    
    return __self;
}

-(instancetype)FCBaseInitWithFrame:(CGRect)rect
{
    id __self = [self FCBaseInitWithFrame:rect];
    
    if (@available(iOS 13.4, *)) {
        self.preferredDatePickerStyle = UIDatePickerStyleWheels;
        
    }
    
    return __self;
}

@end
